package com.alipay.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jie
 * @date 2021/6/13 -21:49
 */
@RestController
@RequestMapping("test")
public class PayController {

    @Value("${alipay.gateway}")
    private String gateway;
    @Value("${alipay.appId}")
    private String appId;
    @Value("${alipay.privateKey}")
    private String privateKey;
    @Value("${alipay.aliPayPublicKey}")
    private String aliPayPublicKey;
    private final String CHARSET = "utf-8";
    private static final String SIGN_TYPE = "RSA2";


    @GetMapping("test")
    public String test() {
        System.out.println("jie");
        return "jie";
    }

    @GetMapping("signVerified")
    public void signVerified() throws AlipayApiException {
        System.out.println("signVerified");
    }



    @GetMapping("pay")
    public String pay() {
//        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",appId,"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCNnakZokKtwwY9awMUFEcGsNQjsWBRxc6F9/IRkbmg6gN44YkMFCtpT5IoYLlWopkWag6ZQ7pzAh47jM+V7GO4TKZBH0Lf++BHZ87HarKZH+efUng9RCgh3wixjROwlocxJigHiwn47A7mzfLVLJU3T28qxPDh/t3uPQc6t/peg/ePvZjHHn33UFmNWqHbO5lrnPqos+yFtZMqwq+hFuTcjljU/XmHkkaCngfk39PNgdlCU+4WBx/mM/10VA4ZGfvKrXgzIOgsUTUKnnPR+cV017LcCz1JAfCq+LpIRy+wP0gHdDyo09axFyIJvLOxROF1mWNowl4enOn23gb273r1AgMBAAECggEAcgWSJIiHxV40RPlHdkBJW7K+a6qiUdTBTw5uwgSiAFTKzOZSsP4AYf9mP8p5D69FMU3dSTvGAFmh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PgG7ttnx3WxADDHyJGeXmv5WW9xenw/JE4+FfUrc8K85eqWyyX8H+gWmkNa5Dh1PUcnA==","json","utf-8","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArFRJcZAzp7mEyt87ko6km3veTY4VvW1SuNEaS3/G5Zcx50KRvJNvmTjzJ6Oo3eAFvId8Ltu0dHnMfqI07jkwMDw8fgammfxpY5aa5LwmnvvhMv9snYYuF3BJQzXaeydRn+duc0L7zL5kuILNbBMECNzdyLYdGQB/xC3UgMocXN7MEBU46uv0TBARxmsP5iPwjyymuJsuypsJrnDfidNe98R792e7CnkK0Yybpyb7Z4mmg1T9pj9aWzPVYQVbGE2XOydXSe2mW42Xp9etux+UQQwllLaLLfbSO7lmvI75VM+M+kBnvpklw9Dqac29Y+ZaY0BY6jKj1Ra4CdS4ESV6DQIDAQAB","RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(gateway, appId, privateKey, "json", CHARSET, aliPayPublicKey, SIGN_TYPE);
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
        alipayRequest.setReturnUrl("http://localhost:8100/test/test");
        alipayRequest.setNotifyUrl("http://localhost:8100/test/signVerified");//在公共参数中设置回跳和通知地址
       /* alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101002\"," +
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数*/

        //填充业务参数
        Map<String, String> map = new HashMap<>();
        map.put("out_trade_no", "20150320010101010");
        map.put("total_amount", "88");
        map.put("subject", "标题");
        map.put("product_code", "QUICK_WAP_PAY");
        String json = JSON.toJSONString(map);
        alipayRequest.setBizContent(json);
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return form;
    }
}
